package com.pug.service.hotel.async;

import com.pug.entity.Hotel;
import com.pug.redis.PugRedisOperator;
import com.pug.service.hotel.IHotelService;
import com.pug.utils.fn.asserts.Vsserts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/3/27 14:03
 */
@Component
@Slf4j
public class HotelRelationService {

    @Autowired
    private PugRedisOperator redisOperator;
    @Autowired
    private IHotelService hotelService;
    @Value("${pug.message.count}")
    private Integer messageCount;

    @Async
    public void countHotelComment(Long hotelId) {
        redisOperator.increment("hotel:comment:count:"+hotelId,1L);
        String countComments = redisOperator.get("hotel:comment:count:" + hotelId);
        if (Vsserts.isNotEmpty(countComments)) {
            Integer cc = Integer.parseInt(countComments);
            if (cc >= messageCount) {
                Hotel hotel = new Hotel();
                hotel.setId(hotelId);
                hotel.setComments(cc);
                hotelService.updateById(hotel);
            }
        }
    }


    @Async
    public void delHotelComment(Long hotelId){
        redisOperator.decrement("hotel:comment:count:"+hotelId,1L);
        String countComments = redisOperator.get("hotel:comment:count:" + hotelId);
        if (Vsserts.isNotEmpty(countComments)) {
            Integer cc = Integer.parseInt(countComments);
            if (cc >= messageCount) {
                Hotel hotel = new Hotel();
                hotel.setId(hotelId);
                hotel.setComments(cc);
                hotelService.updateById(hotel);
            }
        }
    }

    @Async
    public void sendRegMessage() {
        log.info("sendRegMessage - 执行同步");
    }
}
